package com.qdsgjsfk.vision.ui.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.qdsgjsfk.vision.base.BaseDialogFragment;

import com.qdsgjsfk.vision.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * Created by HP on 2018/10/5.
 */

public class RemarkDialog extends BaseDialogFragment {

    private boolean isShowClear, isFromList;

    public static RemarkDialog newInstance(boolean isShowClear, boolean isFromList) {
        Bundle args = new Bundle();
        args.putBoolean( "isShowClear", isShowClear );
        args.putBoolean( "isFromList", isFromList );
        RemarkDialog fragment = new RemarkDialog();
        fragment.setArguments( args );
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView btn_confirm, btn_clear;
    private String reason;
    private EditText edt_reason;
    private int type;
    private String shabixuqiu;
    private RecyclerView recyclerView;
    private List<String> remarkList = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_remark, container );
        btn_confirm = view.findViewById( R.id.btn_confirm );

        btn_confirm.setOnClickListener( v -> {
            if (TextUtils.isEmpty( edt_reason.getText().toString().trim() )) {
                return;
            }
            mListener.onConfirm( edt_reason.getText().toString().trim(), type );
        } );
        btn_clear = view.findViewById( R.id.btn_clear );
        if (isShowClear) {
            btn_clear.setVisibility( View.VISIBLE );
        } else {
            btn_clear.setVisibility( View.GONE );
        }
        btn_clear.setOnClickListener( v -> {
            dismiss();
            mListener.onConfirm( "", 1 );
        } );
        edt_reason = view.findViewById( R.id.edt_reason );
        recyclerView = view.findViewById( R.id.recyclerView );
        GridLayoutManager manager = new GridLayoutManager( getActivity(), 9 );
        manager.setSpanSizeLookup( new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (isFromList) {
                    if (position < 9) {
                        return 3;
                    } else if (position == 9) {
                        return 9;
                    } else {
                        return 9;
                    }

                } else {
                    if (position < 2) {
                        return 4;
                    } else if (position == 2) {
                        return 9;
                    } else {
                        return 9;
                    }
                }


            }
        } );
        recyclerView.setLayoutManager( manager );
        MyAdapter myAdapter = new MyAdapter();
        recyclerView.setAdapter( myAdapter );
        remarkList.clear();
        if (isFromList) {
            remarkList.add( "请假" );
            remarkList.add( "转学" );
            remarkList.add( "实习" );
            remarkList.add( "放假" );
            remarkList.add( "休学" );
            remarkList.add( "出国" );
            remarkList.add( "生病" );
            remarkList.add( "退学" );
            remarkList.add( "已毕业" );
            remarkList.add( "人在外地学籍在本地" );

        } else {
            remarkList.add( "人眼缺陷" );
            remarkList.add( "学生不配合" );
            remarkList.add( "忘记OK镜度数" );
        }

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        assert getArguments() != null;
        isShowClear = getArguments().getBoolean( "isShowClear" );
        isFromList = getArguments().getBoolean( "isFromList" );
    }


    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.CENTER );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull( getDialog().getWindow() ).requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        getDialog().getWindow().setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String reason, int type);

//        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.RemarkHolder> {

        @NonNull
        @Override
        public MyAdapter.RemarkHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RemarkHolder( getLayoutInflater().inflate( R.layout.item_remark, parent, false ) );
        }

        @Override
        public void onBindViewHolder(MyAdapter.RemarkHolder holder, final int position) {
            holder.tv_name.setText( remarkList.get( position ) );
            holder.tv_name.setOnClickListener( v -> {
                dismiss();
                mListener.onConfirm( holder.tv_name.getText().toString().trim(), 1 );
            } );
        }

        @Override
        public int getItemCount() {
            return remarkList.size();
        }


        class RemarkHolder extends RecyclerView.ViewHolder {
            TextView tv_name;

            RemarkHolder(View itemView) {
                super( itemView );
                tv_name = itemView.findViewById( R.id.tv_name );
            }
        }
    }

}
